package com.ftar.urgent.ui;

import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ftar.urgent.R;
import com.ftar.urgent.adapter.CatalogAdapter;
import com.ftar.urgent.constant.ArouterConstant;
import com.ftar.urgent.databinding.ActivityCatalogFourthBinding;
import com.ftar.urgent.model.CatalogModel;
import com.ftar.urgent.model.RootCatalogModel;
import com.ftar.urgent.util.GetCatalog;
import com.ftar.urgent.viewmodel.CommonEvent;
import com.ftar.urgent.viewmodel.SP_Constants;
import com.ftar.urgent.voice.OffLineVoiceCmd;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wei.jin
 * @date: 2023/7/4
 * @email: wei.jin@fanttec.com
 * @description：
 */
@Route(path = ArouterConstant.CATALOG_FOURTH_ACTIVITY)
public class CatalogFourthActivity extends BaseActivity {
	private String TAG = this.getClass().getSimpleName();

	private ActivityCatalogFourthBinding mBinding;
	private CatalogAdapter mCatalogAdapter;
	private List<RootCatalogModel.catalog> mRootList = new ArrayList<>();
	private List<CatalogModel> mCatalogList;
	@Autowired
	public int parentId;

	@Override
	protected ViewDataBinding getChildBinding() {
		mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_catalog_fourth, null, false);
		return mBinding;
	}

	@Override
	protected String voiceHint() {
		return "打开第1/2/3.../6个";
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		init();
	}

	private void init() {
		ARouter.getInstance().inject(this);

		mRootList = GetCatalog.getCatalog(this);
		RootCatalogModel.catalog findCatalog = mRootList.stream()
				.filter(catalog -> catalog.getId() == parentId)
				.findFirst().get();
		/*if(findCatalog != null) {
			mCatalogList = findCatalog.getSub_catalog();
			mCatalogAdapter = new CatalogAdapter(this, mCatalogList);
			mBinding.catalogGridview.setAdapter(mCatalogAdapter);
			mBinding.catalogGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
					itemClick(position);

				}
			});
		}*/
	}

	/**
	 * Catalog item click by keyClick or voice command
	 * @param pos
	 */
	private void itemClick(int pos) {
		if(mCatalogList != null && !mCatalogList.isEmpty()) {
			ARouter.getInstance()
					.build(ArouterConstant.CAPTURE_ACTIVITY)
					.withInt("parentId", parentId)
					.withInt("subId", mCatalogList.get(pos).getId())
					.navigation();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		long selectedId = mBinding.catalogGridview.getSelectedItemId();
		switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_LEFT:
				if(selectedId >= 1) {
					selectedId -= 1;
				}
				setItemSelected((int)selectedId);
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				if(selectedId < mCatalogAdapter.getCount()) {
					selectedId += 1;
				}
				setItemSelected((int)selectedId);
				break;
			case KeyEvent.KEYCODE_F9:
				break;
			default:
				break;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void onAsrCommand(String command) {
		if(!isActivityVisible()) {
			return;
		}
		int pos = 1;
		switch (command) {
			case OffLineVoiceCmd.OfflineVoice.SETTING_SELECT_PROJECT_1:
				pos = 1;
				break;
			case OffLineVoiceCmd.OfflineVoice.SETTING_SELECT_PROJECT_2:
				pos = 2;
				break;
			case OffLineVoiceCmd.OfflineVoice.SETTING_SELECT_PROJECT_3:
				pos = 3;
				break;
			case OffLineVoiceCmd.OfflineVoice.SETTING_SELECT_PROJECT_4:
				pos = 4;
				break;
			case OffLineVoiceCmd.OfflineVoice.SETTING_SELECT_PROJECT_5:
				pos = 5;
				break;
			case OffLineVoiceCmd.OfflineVoice.SETTING_SELECT_PROJECT_6:
				pos = 6;
				break;
		}

		final int focusPos = pos - 1;
		setItemSelected(focusPos);
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				itemClick(focusPos);
			}
		}, 500);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onCommonEvent(CommonEvent event) {
		switch (event.getMsg()) {
			case SP_Constants.EVENT_COMMAND_ASR:
				String command = event.getData();
				onAsrCommand(command);
				break;
			default:
				break;
		}
	}

	private void setItemSelected(int pos) {
		mBinding.catalogGridview.setSelection(pos);
		mCatalogAdapter.setSelectItem(pos);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}
}
